package com.wanhelibs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.wanhe.shoppingcart.bean.SetMealCartItem;
import com.wanhe.shoppingcart.manager.CartManager;
import com.wanhe.shoppingcart.manager.ICartItem;
import com.wanhelibs.R;

import java.util.ArrayList;

/**
 * Created by XieZaiWei on 2017/8/12.
 * 减套餐Adapter
 */

public class ReduceSetMealAdapter extends BaseExpandableListAdapter {

	protected Context						mContext;
	protected LayoutInflater				mLayoutInflater;
	protected ArrayList <SetMealCartItem>	mArrSetMealCartItem;
	private int								mGroupLayout;
	private int								mItemLayout;

	public ReduceSetMealAdapter(Context context, int groupLayout, int itemLayout) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mGroupLayout = groupLayout;
		mItemLayout = itemLayout;
	}

	public void setData(ArrayList <SetMealCartItem> arrSetMealCartItem) {
		if (mArrSetMealCartItem != null) {
			mArrSetMealCartItem.clear();
			mArrSetMealCartItem = null;
		}
		mArrSetMealCartItem = arrSetMealCartItem;
		notifyDataSetChanged();
	}

	public void clean() {
		if (mArrSetMealCartItem != null) {
			mArrSetMealCartItem.clear();
			mArrSetMealCartItem = null;
		}
		notifyDataSetChanged();
	}

	@Override
	public int getGroupCount() {
		if (mArrSetMealCartItem == null) {
			return 0;
		}
		return mArrSetMealCartItem.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public SetMealCartItem getGroup(int groupPosition) {
		return mArrSetMealCartItem.get(groupPosition);
	}

	@Override
	public ICartItem getChild(int groupPosition, int childPosition) {
		return mArrSetMealCartItem.get(groupPosition).getArrChildItem().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
			ViewGroup parent) {
		GroupHolder holder;
		if (convertView == null) {
			holder = new GroupHolder();
			convertView = mLayoutInflater.inflate(mGroupLayout, null);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_reduce = (TextView) convertView.findViewById(R.id.tv_reduce);
			convertView.setTag(holder);

		} else {
			holder = (GroupHolder) convertView.getTag();
		}
		final SetMealCartItem setMealCartItem = mArrSetMealCartItem.get(groupPosition);
		holder.tv_name.setText(setMealCartItem.getName());
		holder.tv_reduce.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				CartManager.getInstance().reduceItem(setMealCartItem, 1);
			}
		});

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
			View convertView, ViewGroup parent) {
		ChildHolder holder;
		if (convertView == null) {
			holder = new ChildHolder();
			convertView = mLayoutInflater.inflate(R.layout.reduce_set_meal_child, null);
			holder.lv_dish = (ListView) convertView.findViewById(R.id.lv_dish);
			convertView.setTag(holder);
		} else {
			holder = (ChildHolder) convertView.getTag();
		}
		holder.lv_dish.setAdapter(new ReduceSetMealChildAdapter(mContext,
				mArrSetMealCartItem.get(groupPosition).getArrChildItem(), mItemLayout));

		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	protected static class GroupHolder {
		public TextView	tv_name;
		public TextView	tv_reduce;
	}

	protected static class ChildHolder {
		public ListView lv_dish;

	}
}
